<?php
class MiscUtils {
  public static function dump($obj, $what = NULL) {
    if (defined ('DEBUG_ENABLED') && ! DEBUG_ENABLED) {
      return;
    }
    echo "<pre>\n";
    if (is_null ($obj)) {
      echo ($what !== NULL) ? "$what=!NULL!" : '!NULL!';
    } else if ($obj === '') {
      echo ($what !== NULL) ? "$what=!EMPTY!" : '!EMPTY!';
    } else if ($obj === false) {
      echo ($what !== NULL) ? "$what=!FALSE!" : '!FALSE!';
    } else {
      if ($what !== NULL) {
        echo "$what=<br>";
      }
      echo htmlspecialchars (print_r ($obj, true));
    }
    echo "</pre>\n";
  }
  
  public static function getParam($itm, $default = '', $trimText = false) {
    if (isset($_POST[$itm])) {
    	//echo '1-' . $_POST[$itm];
      return ($trimText) ? trim($_POST[$itm]) : $_POST[$itm];
    } else if (isset($_GET[$itm])) {
    	//echo '2-' . $_GET[$itm];
      return ($trimText) ? trim($_GET[$itm]) : $_GET[$itm];
    } else {
    	//echo 'sss';
      return $default;
    }
  }
  
  public static function encode($obj) {
    if ($obj) {
      $item = urlencode(urlencode($obj));
    }
    return $item;
  }
  public static function unique() {
    $unique = 'u' . (mktime() * rand());
    return $unique;
  }
  
  public static function obj2arr($obj) {
    $arr = array();
    
    foreach ($obj as $key => $value) {
      $tmp = new stdClass();
      $value = (is_object($value) && ($value->year || $value->month || $value->date)) ? SimpleDate::toStamp($value) : $value;
      
      if ((gettype($value) == 'NULL') && is_null($value)) {
        $value = 0;
      }
      if ((gettype($value) == 'boolean') && empty($value)) {
        $value = 0;
      }
      
      if (is_object($value) || is_array($value) || $key == '_added' || $key == '_deleted' || $key == '_exist' || $key == '_text' || $key == '_method' || $key == '_received') {
        continue;
      }
      
      $tmp->key = $key;
      $tmp->value = mysql_escape_string($value);
      $arr[] = $tmp;
    }
    return $arr;
  }
  
  public static function obj2Ins($table, $obj) {
    $arr = MiscUtils::obj2arr($obj);
    
    $sql = "INSERT INTO `$table` (`id`, ";
    $val = "(NULL, ";
    
    foreach ($arr as $idx => $tmp) {
      if ($tmp->key == 'id') {
        continue;
      }
      $sql .= "`$tmp->key`";
      $val .= isset($tmp->value) ? "'$tmp->value'" : "NULL";
      
      if ($idx < (count($arr) - 1)) {
        $sql .= ', ';
        $val .= ', ';
      } else {
        $sql .= ') ';
        $val .= ')';
      }
    }
    
    $sql .= "VALUE $val";
    return $sql;
  }
  
  public static function obj2Upd($table, $obj) {
    $arr = MiscUtils::obj2arr($obj);
    $sql = "UPDATE `$table` SET ";
    
    foreach ($arr as $idx => $tmp) {
      if ($tmp->key == 'id') {
        continue;
      }
      
      $sql .= isset($tmp->value) ? "`$tmp->key` = '$tmp->value'" : "`$tmp->key` = NULL";
      
      if ($idx < (count($arr) - 1)) {
        $sql .= ', ';
      }
    }
    
    $sql .= " WHERE id = $obj->id";
    return $sql;
  }
  
  public static function obj2Del($table, $obj) {
    $arr = MiscUtils::obj2arr($obj);
    
    $sql = "DELETE FROM `$table` WHERE id = $obj->id";
    
    return $sql;
  }
}
?>